# Maintainer: Yusuke Sasaki <y_sasaki@nuem.nagoya-u.ac.jp>

_realname=googletest
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-gtest")
pkgver=1.10.0
pkgrel=1
pkgdesc="Googletest (mingw-w64)"
arch=('any')
url="https://github.com/google/googletest"
license=('custom:BSD3')
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/googletest/archive/release-${pkgver}.tar.gz"
        01-python3-fixes.patch
        02-revert-changes-to-pkgconfig-files.patch
        03-fix-pkgconfig-files.patch)
sha256sums=('9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb'
            '4da51c251de5f57067d2492860b17b5a11f6ed60049a0be49421657e1228e642'
            'a1efd397bddaa9e584d1133a3b1fc7cef444c8e41451b5985816f8a0960c7f53'
            '7112f4bebfa4bd6658c5c886242d0dff820d00ff9204edb667ff64618107f84a')

prepare() {
  cd googletest-release-${pkgver}
  patch -p1 -i ${srcdir}/01-python3-fixes.patch
  patch -p1 -i ${srcdir}/02-revert-changes-to-pkgconfig-files.patch
  patch -p1 -i ${srcdir}/03-fix-pkgconfig-files.patch
}

build() {
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_SKIP_RPATH=ON \
    ../googletest-release-${pkgver}

  make
  ${MINGW_PREFIX}/bin/python ../googletest-release-${pkgver}/googlemock/scripts/fuse_gmock_files.py ../googletest-release-${pkgver}/googlemock fused-gmock-src
  ${MINGW_PREFIX}/bin/python ../googletest-release-${pkgver}/googletest/scripts/fuse_gtest_files.py ../googletest-release-${pkgver}/googletest fused-gtest-src
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"

  mkdir -p "${pkgdir}"/${MINGW_PREFIX}/src/{gmock,gtest}/{cmake,src}
  install -m 0644 fused-gtest-src/gtest/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/src/
  install -m 0644 fused-gmock-src/gmock/* "${pkgdir}"${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 fused-gmock-src/*.cc "${pkgdir}"${MINGW_PREFIX}/src/gmock/src/
  install -m 0644 ${srcdir}/googletest-release-${pkgver}/googletest/cmake/* "${pkgdir}"${MINGW_PREFIX}/src/gtest/cmake/
  
  install -Dm 644 ${srcdir}/googletest-release-${pkgver}/googlemock/scripts/generator/{*.py,LICENSE,README*} -t "${pkgdir}${MINGW_PREFIX}/share/gmock/generator"
  install -Dm 644 ${srcdir}/googletest-release-${pkgver}/googlemock/scripts/generator/cpp/* -t "${pkgdir}${MINGW_PREFIX}/share/gmock/generator/cpp"
  
  install -Dm 0644 ${srcdir}/googletest-release-${pkgver}/googletest/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gtest/LICENSE
  install -Dm 0644 ${srcdir}/googletest-release-${pkgver}/googlemock/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/gmock/LICENSE
}
